package com.sxbbc.plat.controller.jz;

import java.io.File;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.utils.FileUtils;
import com.sxbbc.common.core.utils.MultipartFileToFile;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.files.RestUploadFileInfo;
import com.sxbbc.common.basics.files.UploadUtils;
import com.sxbbc.common.core.entity.jz.BankType;
import com.sxbbc.common.core.entity.jz.SysCashDetails;
import com.sxbbc.common.core.entity.jz.SysCashRule;
import com.sxbbc.common.core.enums.CashTypeEnums;
import com.sxbbc.common.core.service.IBankTypeService;
import com.sxbbc.common.core.service.ISysCashDetailsService;
import com.sxbbc.common.core.service.ISysCashRuleService;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.plat.common.CashMapping;
import com.sxbbc.plat.util.SystemLog;

/**
 * 提现管理 控制层
 *
 * @author leehao
 * @date 2019-04-29 09:32
 */
@Controller
public class CashController {
    @Autowired
    private IBankTypeService bankTypeService;
    @Autowired
    private ISysCashRuleService cashRuleService;
    @Autowired
    private ISysCashDetailsService cashDetailsService;

    /**
     * 提现申请审核列表
     *
     * @param page    分页条件
     * @param details 查询条件
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = CashMapping.SHOW_CASH_DETAILS)
    public ModelAndView showCashDetails(BasePage<SysCashDetails> page, SysCashDetails details) {
        BasePage<SysCashDetails> pageInfo = cashDetailsService.queryCashDetails(page, details);
        List<CashTypeEnums> typeTypeList = CashTypeEnums.queryList();
        ModelAndView view = new ModelAndView(CashMapping.SHOW_CASH_DETAILS);
        view.addObject("pageInfo", pageInfo);
        view.addObject("detail", details);
        view.addObject("typeTypeList", typeTypeList);
        return view;
    }

    /**
     * 提现申请审核
     *
     * @param id     记录id
     * @param status 2通过，3驳回
     * @param remark 驳回备注
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = CashMapping.EDIT_CASH_APPLY)
    @ResponseBody
    @SystemLog(methods = "审核提现申请", module = "提现管理")
    public RestResponse editCashApplyStatus(Integer id, Integer status, String remark) {
        return cashDetailsService.editCashApplyStatus(id, status, remark);
    }

    /**
     * 银行卡列表
     *
     * @param page 分页条件
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = CashMapping.SHOW_BANK_LIST)
    public ModelAndView showBankList(BasePage<BankType> page) {
        QueryWrapper<BankType> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StaticUtils.STATUS_YES);
        List<BankType> list = bankTypeService.list(wrapper);
        ModelAndView view = new ModelAndView(CashMapping.SHOW_BANK_LIST);
        view.addObject("list", list);
        return view;
    }

    /**
     * 新增编辑银行卡页面
     *
     * @param id id
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = CashMapping.SHOW_BANK_EDIT)
    public ModelAndView showEditBank(Integer id) {
        BankType bank = bankTypeService.getById(id);
        ModelAndView view = new ModelAndView(CashMapping.SHOW_BANK_EDIT);
        view.addObject("bank", bank);
        return view;
    }

    /**
     * 新增/编辑银行卡
     *
     * @param imgUrl 银行卡logo
     * @param bank   对象
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = CashMapping.EDIT_BANK_TYPE)
    @ResponseBody
    @SystemLog(methods = "新增/编辑银行卡", module = "提现管理")
    public RestResponse editBank(MultipartFile imgUrl, BankType bank) {
        if (StringUtils.isBlank(bank.getBankName())) {
            return GetRest.getFail("请输入银行卡名称");
        }
        if (bank.getCountryId() == null) {
            return GetRest.getFail("请选择所属国家");
        }
        if (bank.getId() == null && imgUrl == null) {
            return GetRest.getFail("请上传银行卡logo");
        }
//        RestUploadFileInfo result = UploadUtils.getInstance().uploadFileQN(imgUrl);
//        if (result.isStatus()) {
//            bank.setLogo(result.getData());
//        } else if (!result.isStatus() && imgUrl != null) {
//            return GetRest.getFail("logo上传失败：" + result.getDesc());
//        }
        try {
            File fe= MultipartFileToFile.multipartFileToFile(imgUrl);
            String url = FileUtils.up(fe);
            bank.setLogo(url);
        }catch (Exception e){
            return GetRest.getFail("logo上传失败");
        }
        boolean save = bankTypeService.saveOrUpdate(bank);
        if (!save) {
            throw new ThrowJsonException("保存银行卡失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    /**
     * 删除银行卡类型
     *
     * @param id 记录id
     * @return 成功true反之false
     * @author leehao
     */
    @RequestMapping(value = CashMapping.DELETE_BANK)
    @ResponseBody
    @SystemLog(methods = "删除银行卡类型", module = "提现管理")
    public boolean deleteBankById(Integer id) {
        return bankTypeService.removeById(id);
    }

    /**
     * 提现规则页面
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = CashMapping.SHOW_CASH_SETTING)
    public ModelAndView showCashSetting() {
        SysCashRule queryCashRule = new SysCashRule();
        // 用户提现规则
        queryCashRule.setRoleType(CashTypeEnums.CUSTOMER.getId());
        SysCashRule customerCashRule = cashRuleService.getOne(cashRuleService.queryWrapper(queryCashRule));
        // 商家提现规则
        queryCashRule.setRoleType(CashTypeEnums.BUSINESS.getId());
        SysCashRule businessCashRule = cashRuleService.getOne(cashRuleService.queryWrapper(queryCashRule));
        // 代理提现规则
        queryCashRule.setRoleType(CashTypeEnums.AGENT.getId());
        SysCashRule agentCashRule = cashRuleService.getOne(cashRuleService.queryWrapper(queryCashRule));
        ModelAndView view = new ModelAndView(CashMapping.SHOW_CASH_SETTING);
        view.addObject("customerRule", customerCashRule);
        view.addObject("businessRule", businessCashRule);
        view.addObject("agentCashRule", agentCashRule);
        return view;
    }

    /**
     * 保存提现规则
     *
     * @param cashRule 对象
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = CashMapping.EDIT_CASH_SETTING)
    @ResponseBody
    @SystemLog(methods = "保存提现规则", module = "提现管理")
    public RestResponse editCashSetting(SysCashRule cashRule) {
        return cashRuleService.editCashSetting(cashRule);
    }
}
